package com.jerehedu.myapplication34.activity;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.Switch;
import android.widget.TextView;
import android.widget.Toast;

import com.jerehedu.myapplication34.R;

public class SettingActivity extends AppCompatActivity {
    private  String[] str=new String[]{"小", "中", "大", "特大"};
    private String[] str1=new String[]{"0分钟","60分钟","90分钟","120分钟"};
    private RelativeLayout textSize,restTime,version,exitLogin;
    private TextView size,time,login;
    private Switch switchPage,isFullScreen,isNightMode;
    private ImageView back;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_setting);
        back=(ImageView)findViewById(R.id.back);
        version=(RelativeLayout)findViewById(R.id.version);
        textSize=(RelativeLayout)findViewById(R.id.textSize);
        restTime=(RelativeLayout)findViewById(R.id.restTime);
        exitLogin=(RelativeLayout)findViewById(R.id.exitLogin);
        switchPage=(Switch)findViewById(R.id.switchPage);
        isFullScreen=(Switch)findViewById(R.id.isFullScreen);
        isNightMode=(Switch)findViewById(R.id.isNightMode);
        size=(TextView)findViewById(R.id.size);
        time=(TextView)findViewById(R.id.time);
        login=(TextView)findViewById(R.id.login);
        SharedPreferences sp=getSharedPreferences("SPTextSize",Context.MODE_PRIVATE);
        int sizeNum=sp.getInt("textSize",1);
        size.setText(str[sizeNum]);
        SharedPreferences sp1=getSharedPreferences("SPRestTime",Context.MODE_PRIVATE);
        int timeNum=sp1.getInt("restTime",0);
        time.setText(str1[timeNum]);
        textSize.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                final AlertDialog.Builder builder = new AlertDialog.Builder(SettingActivity.this);
                SharedPreferences sp = getSharedPreferences("SPTextSize", Context.MODE_PRIVATE);
                int sizeNum2 = sp.getInt("textSize", 1);
                builder.setTitle("字体大小")
                        .setSingleChoiceItems(str, sizeNum2, new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                SharedPreferences sp = getSharedPreferences("SPTextSize", Context.MODE_PRIVATE);
                                SharedPreferences.Editor editor = sp.edit();
                                editor.putInt("textSize", which);
                                editor.commit();
                                size.setText(str[which]);
                                dialog.dismiss();
                            }
                        })
                        .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                dialog.dismiss();
                            }
                        });
                builder.create().show();
            }
        });

        restTime.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                final AlertDialog.Builder builder=new AlertDialog.Builder(SettingActivity.this);
                SharedPreferences sp=getSharedPreferences("SPRestTime",Context.MODE_PRIVATE);
                int timeNum2=sp.getInt("restTime",0);
                builder.setTitle("休息时间")
                        .setSingleChoiceItems(str1, timeNum2, new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                SharedPreferences sp=getSharedPreferences("SPRestTime", Context.MODE_PRIVATE);
                                SharedPreferences.Editor editor=sp.edit();
                                editor.putInt("restTime",which);
                                editor.commit();
                                time.setText(str1[which]);
                                dialog.dismiss();
                            }
                        })
                        .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                dialog.dismiss();
                            }
                        });
                builder.create().show();
            }
        });

        SharedPreferences sp2 = getSharedPreferences("SPSwitchPage", Context.MODE_PRIVATE);
        int pageNum=sp2.getInt("switchPage",0);
        if(pageNum==0){
            switchPage.setChecked(false);
        }else {
            switchPage.setChecked(true);
        }
        switchPage.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                SharedPreferences sp = getSharedPreferences("SPSwitchPage", Context.MODE_PRIVATE);
                SharedPreferences.Editor editor = sp.edit();
                if (isChecked) {
                    editor.putInt("switchPage", 1);
                    editor.commit();
                } else {
                    editor.putInt("switchPage", 0);
                    editor.commit();
                }
            }
        });

        SharedPreferences sp3 = getSharedPreferences("SPIsFullScreen", Context.MODE_PRIVATE);
        int screenNum=sp3.getInt("isFullScreen",0);
        if(screenNum==0){
            isFullScreen.setChecked(false);
        }else {
            isFullScreen.setChecked(true);
        }
        isFullScreen.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                SharedPreferences sp=getSharedPreferences("SPIsFullScreen", Context.MODE_PRIVATE);
                SharedPreferences.Editor editor=sp.edit();
                if(isChecked){
                    editor.putInt("isFullScreen",1);
                    editor.commit();
                }else{
                    editor.putInt("isFullScreen",0);
                    editor.commit();
                }
            }
        });

        SharedPreferences sp4 = getSharedPreferences("SPIsNightMode", Context.MODE_PRIVATE);
        int nightmodeNum=sp4.getInt("isNightMode",0);
        if(nightmodeNum==0){
            isNightMode.setChecked(false);
        }else {
            isNightMode.setChecked(true);
        }
        isNightMode.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                SharedPreferences sp = getSharedPreferences("SPIsNightMode", Context.MODE_PRIVATE);
                SharedPreferences.Editor editor = sp.edit();
                if (isChecked) {
                    editor.putInt("isNightMode", 1);
                    editor.commit();
                } else {
                    editor.putInt("isNightMode", 0);
                    editor.commit();
                }
            }
        });

        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        version.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                final AlertDialog.Builder builder=new AlertDialog.Builder(SettingActivity.this);
                builder.setMessage("当前为最新版本")
                        .setNegativeButton("退出", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                dialog.dismiss();
                            }
                        });
                builder.create().show();
            }
        });

        SharedPreferences sp5=getSharedPreferences("SPIsLogin", Context.MODE_PRIVATE);
        int loginNum=sp5.getInt("isLogin", 0);
        String userName=sp5.getString("userName", null);
        if(loginNum==0){
            login.setText("未登录");
        }else {
           login.setText("退出登录");
        }
        exitLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(login.getText().equals("未登录")){
                    Toast.makeText(SettingActivity.this,"请先登录",Toast.LENGTH_SHORT).show();
                }else{
                    final AlertDialog.Builder builder=new AlertDialog.Builder(SettingActivity.this);
                    SharedPreferences sp=getSharedPreferences("SPIsLogin", Context.MODE_PRIVATE);
                    final SharedPreferences.Editor editor=sp.edit();
                    builder.setTitle("账号管理")
                            .setMessage("确定退出登录")
                            .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog, int which) {
                                    editor.putInt("isLogin",0);
                                    editor.commit();
                                    login.setText("未登录");
                                    dialog.dismiss();
                                }
                            })
                            .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog, int which) {
                                    dialog.dismiss();
                                }
                            });
                    builder.create().show();
                }

            }
        });
    }
}
